package com.example.nv21tobitmap;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    /**
     * 需要获取的权限列表
     */
    private String[] permissions = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET,
            Manifest.permission.MODIFY_AUDIO_SETTINGS,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
    };

    /**
     * 动态申请权限
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initPermissions() {
        if (isLacksPermission()) {
            //动态申请权限 , 第二参数是请求吗
            requestPermissions(permissions, PERMISSION_REQUEST_CODE);
        }
    }

    /**
     * 动态申请权限的请求码
     */
    private static final int PERMISSION_REQUEST_CODE = 888;

    /**
     * 判断是否有 permissions 中的权限
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean isLacksPermission() {
        for (String permission : permissions) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        }
        return false;
    }

    private ViewGroup mRootLayer;
    private Button mBtnYuv420p, mBtnNV12, mBtnNV21;
    private SurfaceView mSurfaceView;
    private NativeYUV mNativeYUV = new NativeYUV();


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化权限
        initPermissions();
        setContentView(R.layout.activity_main);
        setupView();
    }


    public void onYUV420PToRGB24(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "1629702734580nv21");
        mNativeYUV.yuv2rgb(file.getAbsolutePath(), NativeYUV.Type.YUV420P_TO_RGB24, 720, 1280, mSurfaceView.getHolder().getSurface());
    }

    public void onNV12ToRGB24(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "1629702734580nv21");
        mNativeYUV.yuv2rgb(file.getAbsolutePath(), NativeYUV.Type.NV12_TO_RGB24, 720, 1280, mSurfaceView.getHolder().getSurface());
    }

    public void onNV21ToRGB24(View view) {
        File file = new File(Environment.getExternalStorageDirectory(), "1629705411662nv21");
//        mNativeYUV.yuv2rgb(file.getAbsolutePath(), NativeYUV.Type.NV21_TO_RGB24, 1536, 2048, mSurfaceView.getHolder().getSurface());
        mNativeYUV.yuv2rgb(file.getAbsolutePath(), NativeYUV.Type.NV21_TO_RGB24, 720, 1280,mSurfaceView.getHolder().getSurface());
    }







    private void setupView() {
        mRootLayer = (ViewGroup) findViewById(R.id.native_yub2rgb_root_layer);
        mBtnYuv420p = (Button) findViewById(R.id.btn_yuv420p);
        mBtnNV12 = (Button) findViewById(R.id.btn_nv12);
        mBtnNV21 = (Button) findViewById(R.id.btn_nv21);










//        mSurfaceView = new SurfaceView(this);
//        mRootLayer.addView(mSurfaceView);
//        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(SurfaceHolder holder) {
//                mNativeYUV = new NativeYUV();
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//
//            }
//
//            @Override
//            public void surfaceDestroyed(SurfaceHolder holder) {
//
//            }
//        });




    }


    private String path = "/storage/emulated/0/";

    public void onNv21ToNv12(View view) {


    }

}